package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.DictDao;
import com.khyr.entity.DictType;
import com.khyr.service.DictDataService;
import com.khyr.service.DictService;
import com.khyr.vo.DictTypeVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictDao;

    @Override
    public R<Page<DictType>> findDictTypeByCond(DictTypeVo dictTypeVo) {
        //分页
        Page<DictType> page = new Page<>(dictTypeVo.getPageNum(), dictTypeVo.getPageSize());
        //模糊
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(dictTypeVo.getDictName())){
            queryWrapper.like("dict_name", dictTypeVo.getDictName());
        }
        if (StringUtils.hasText(dictTypeVo.getDictType())){
            queryWrapper.like("dict_type", dictTypeVo.getDictType());
        }
        if (StringUtils.hasText(dictTypeVo.getStatus())){
            queryWrapper.like("status", dictTypeVo.getStatus());
        }
        if (StringUtils.hasText(dictTypeVo.getBeginTime())&& StringUtils.hasText(dictTypeVo.getEndTime())){
            queryWrapper.between("create_time", dictTypeVo.getBeginTime(), dictTypeVo.getEndTime());
        }

        Page<DictType> dictTypePage = dictDao.selectPage(page, queryWrapper);
        if (dictTypePage != null){
            return new R<>(200,"查询成功", dictTypePage);
        }
        return new R<>(400,"查询失败", null);
    }

    @Override
    public R<Integer> addDictType(DictType dictType) {
        if (dictType != null){
            int insert = dictDao.insert(dictType);
            if (insert > 0){
                return new R<>(200,"新增成功", insert);
            }
        }
        return new R<>(500,"新增失败", null);
    }

    @Override
    public R<Integer> updateDictType(DictType dictType) {
        if (dictType != null){
            int update = dictDao.updateById1(dictType);
            if (update > 0){
                return new R<>(200,"修改成功", update);
            }
        }
        return new R<>(500,"修改失败", null);
    }

    @Override
    public R<Integer> deleteDictTypeByIds(List<Long> ids) {
        if (ids != null){
            int delete = dictDao.deleteIds(ids);
            if (delete > 0){
                return new R<>(200,"删除成功", delete);
            }
        }
        return new R<>(500,"删除失败", null);
    }

    @Override
    public R<DictType> getDictTypeByDictTypeId(Long id) {
        if (id != null){
            QueryWrapper<DictType> id1 = new QueryWrapper<>();
            id1.eq("dict_id", id);
            DictType dictType = dictDao.selectOne(id1);
            if (dictType != null){
                return new R<>(200,"查询成功", dictType);
            }
        }
        return new R<>(500,"查询失败", null);
    }


}
